package com.myapp.web.struts2.components;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.components.UIBean;
import org.apache.struts2.components.Param.UnnamedParametric;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import com.opensymphony.xwork2.util.ValueStack;

@StrutsTag(name="fieldTemplate", tldTagClass="com.myapp.web.struts2.ui.FieldTemplateTag", description="Render field template")
public class FieldTemplateBean extends UIBean implements UnnamedParametric {

	private List<String> values = new ArrayList<String>();
	
	private String fieldName; 
	
    private boolean escape = true;

    public FieldTemplateBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
        super(stack, request, response);
    }

    private String template = "fielderror";

	@Override
    protected String getDefaultTemplate() {
    	 return template;
    }

    @Override
    protected void evaluateExtraParams() {
        super.evaluateExtraParams();

        if (values != null)
            addParameter("values", values);

        addParameter("escape", escape);
    }

    @Override
    public void addParameter(Object value) {
        if (value != null) {
        	values.add(value.toString());
        }
    }

    public List<String> getValues() {
        return values;
    }

    @StrutsTagAttribute(description="Field name for single field attribute usage", type="String")
    public void setFieldName(String fieldName) {
    	this.fieldName = fieldName;
        addParameter(fieldName);
    }

    public String getFieldName(){
    	return fieldName ;
    }

    @StrutsTagAttribute(description=" Whether to escape HTML", type="Boolean", defaultValue="true")
    public void setEscape(boolean escape) {
        this.escape = escape;
    }	
    
    @StrutsTagAttribute(description="render the template", type="String")
    public void setTemplate(String templ) {
        this.template = templ;
    }    
}
